package com.huluobo.a2307monthdemo.adapter

import android.animation.Animator
import android.animation.AnimatorListenerAdapter
import android.animation.ObjectAnimator
import android.media.Image
import android.view.View
import android.view.animation.LinearInterpolator
import android.widget.ImageView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.huluobo.a2307monthdemo.R
import com.huluobo.a2307monthdemo.utils.ImageUtils
import com.huluobo.lib_base.entity.VideoBean
import com.shuyu.gsyvideoplayer.video.StandardGSYVideoPlayer

/**
 *  Created by LC on 2025/9/28.
 */
class VideoAdapter : BaseQuickAdapter<VideoBean, BaseViewHolder>(R.layout.item_videos) {
    override fun convert(holder: BaseViewHolder, item: VideoBean) {
        val player = holder.getView<StandardGSYVideoPlayer>(R.id.item_videos_player)
        player.backButton.visibility = View.GONE
        player.setUp(item.videopath, true, "")
        val imageView = ImageView(context)
        ImageUtils.loadImage(context, item.videomainimg, imageView)
        player.thumbImageView = imageView

        ImageUtils.loadCircleImage(context, item.headpath, holder.getView(R.id.item_videos_header_iv))
        if (item.guanzhu == 0) {
            holder.getView<ImageView>(R.id.item_videos_add_iv).visibility = View.VISIBLE
        } else {
            holder.getView<ImageView>(R.id.item_videos_add_iv).visibility = View.GONE
        }

        if (item.dianzan == 0) {
            holder.setImageResource(R.id.item_videos_like_iv, R.drawable.ic_xihuan)
        } else {
            holder.setImageResource(R.id.item_videos_like_iv, R.drawable.ic_xihuan)
            val animatorAlphaIn =
                ObjectAnimator.ofFloat(
                    holder.getView<ImageView>(R.id.item_videos_like_iv),
                    "alpha",
                    1f,
                    0f
                )
            animatorAlphaIn.duration = 1000
            animatorAlphaIn.start()

            val animatorAlphaOut = ObjectAnimator.ofFloat(
                holder.getView<ImageView>(R.id.item_videos_like_iv),
                "alpha",
                0f,
                1f
            )
            animatorAlphaOut.duration = 1000

            animatorAlphaIn.addListener(object : AnimatorListenerAdapter() {
                override fun onAnimationEnd(animation: Animator) {
                    super.onAnimationEnd(animation)
                    animatorAlphaOut.start()
                }
            })

            animatorAlphaOut.addListener(object : AnimatorListenerAdapter() {
                override fun onAnimationStart(animation: Animator) {
                    super.onAnimationStart(animation)
                    holder.getView<ImageView>(R.id.item_videos_like_iv).setImageResource(R.drawable.ic_xihuanred)
                }
            })
        }

        val musicPlayer = holder.getView<ImageView>(R.id.item_videos_music_player_iv)
        ImageUtils.loadCircleImage(context, item.videomainimg, musicPlayer)

        val animator = ObjectAnimator.ofFloat(musicPlayer, "Rotation", 0f, 360f)
        animator.duration = 3000
        animator.repeatCount = -1
        animator.interpolator = LinearInterpolator()
        animator.start()

        if (item.address.isNullOrEmpty()) {
            holder.setText(R.id.item_videos_address_tv, "定位失败")
        } else {
            holder.setText(R.id.item_videos_address_tv, item.address)
        }
        holder.setText(R.id.item_videos_time_tv, item.publishtime)
        holder.setText(R.id.item_videos_authname_tv, item.authname)
        holder.setText(R.id.item_videos_caption_tv, item.caption)
        holder.setText(R.id.item_videos_pao_tv, "${item.authname}原创作品")
    }
}